#define _CRT_SECURE_NO_WARNINGS   1

#include<iostream>
#include<string>
#include<vector>
#include<queue>
using namespace std;
int main()
{
	int n;
	int m;
	int xs, ys, xt, yt;
	cin >> n >> m;
	cin >> xs >> ys >> xt >> yt;
	string s;
	vector<vector<char>>MiGong(n, vector<char>(m));
	for (int i = 0; i<n; i++)
	{
		cin >> s;
		for (int j = 0; j<m; j++)
		{
			MiGong[i][j] = s[j];
		}
	}
	if (MiGong[xt - 1][yt - 1] == '*')
	{
		cout << -1 << endl;
		return 0;
	}
	else
	{
		vector<vector<bool>> visit(n, vector<bool>(m, false));
		queue<pair<int, int>>que;
		que.push(make_pair(xs - 1, ys - 1));
		visit[xs - 1][ys - 1] = true;
		int ans = 0;
		bool flag = false;
		while (!que.empty())
		{
			queue<pair<int, int>>tmp;
			while (!que.empty())
			{
				int x = que.front().first;
				int y = que.front().second;
				if (x == (xt - 1) && y == (yt - 1))
				{
					cout << ans << endl;
					flag = true;
					break;
				}
				que.pop();
				if (x>0 && MiGong[x - 1][y] == '.'&&!visit[x - 1][y])
				{
					tmp.push(make_pair(x - 1, y));
					visit[x - 1][y] = true;
				}
				if (x<(n - 1) && MiGong[x + 1][y] == '.'&&!visit[x + 1][y])
				{
					tmp.push(make_pair(x + 1, y));
					visit[x + 1][y] = true;
				}
				if (y>0 && MiGong[x][y - 1] == '.'&&!visit[x][y - 1])
				{
					tmp.push(make_pair(x, y - 1));
					visit[x][y - 1] = true;
				}
				if (y<(m - 1) && MiGong[x][y + 1] == '.'&&!visit[x][y + 1])
				{
					tmp.push(make_pair(x, y + 1));
					visit[x][y + 1] = true;
				}
			}
			que = tmp;
			ans++;
		}
		if (!flag)
			cout << -1 << endl;
		return 0;
	}
}